'use strict';

const Controller = require('egg').Controller;

class ReminderController extends Controller {
  // 获取所有提醒
  async index() {
    const { ctx, service } = this;



    try {
      const { page = 1, pageSize = 20, type, status } = ctx.query;
      const token = ctx.get('Authorization')?.replace('Bearer ', '') ||
      ctx.query.token ||
      ctx.request.body?.token;
      console.log(ctx.state.user,'ctx.state.user',token)
      const reminders = await service.reminder.getList(ctx.state.user?.id || 1, {
        page: parseInt(page),
        pageSize: parseInt(pageSize),
        type,
        status
      });

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: reminders,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 创建提醒
  async create() {
    const { ctx, service } = this;



    try {
      const reminderData = {
        ...ctx.request.body,
        user_id: ctx.state.user?.id || 1
      };

      const reminder = await service.reminder.create(reminderData);

      ctx.body = {
        code: 200,
        message: '创建成功',
        data: reminder,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 更新提醒
  async update() {
    const { ctx, service } = this;



    try {
      const { id } = ctx.params;
      const reminder = await service.reminder.update(
        id,
        ctx.state.user?.id || 1,
        ctx.request.body
      );

      ctx.body = {
        code: 200,
        message: '更新成功',
        data: reminder,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 删除提醒
  async destroy() {
    const { ctx, service } = this;



    try {
      const { id } = ctx.params;
      await service.reminder.delete(id, ctx.state.user?.id || 1);

      ctx.body = {
        code: 200,
        message: '删除成功',
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 标记提醒为已完成
  async markAsCompleted() {
    const { ctx, service } = this;



    try {
      const { id } = ctx.params;
      await service.reminder.markAsCompleted(id, ctx.state.user?.id || 1);

      ctx.body = {
        code: 200,
        message: '标记成功',
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取今日提醒
  async todayReminders() {
    const { ctx, service } = this;



    try {
      const reminders = await service.reminder.getTodayReminders(ctx.state.user?.id || 1);

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: reminders,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 创建洗涤提醒
  async createLaundryReminder() {
    const { ctx, service } = this;



    try {
      const { clothingId, reminderDate, notes } = ctx.request.body;
      
      if (!clothingId) {
        ctx.throw(400, '缺少衣服ID');
      }

      const reminder = await service.reminder.createLaundryReminder(
        ctx.state.user?.id || 1,
        { clothingId, reminderDate, notes }
      );

      ctx.body = {
        code: 200,
        message: '洗涤提醒创建成功',
        data: reminder,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 创建换季提醒
  async createSeasonalReminder() {
    const { ctx, service } = this;



    try {
      const { season, reminderDate, notes } = ctx.request.body;
      
      if (!season) {
        ctx.throw(400, '缺少季节信息');
      }

      const reminder = await service.reminder.createSeasonalReminder(
        ctx.state.user?.id || 1,
        { season, reminderDate, notes }
      );

      ctx.body = {
        code: 200,
        message: '换季提醒创建成功',
        data: reminder,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 创建购物提醒
  async createShoppingReminder() {
    const { ctx, service } = this;



    try {
      const { items, reminderDate, budget, notes } = ctx.request.body;
      
      if (!items || !Array.isArray(items) || items.length === 0) {
        ctx.throw(400, '购物清单不能为空');
      }

      const reminder = await service.reminder.createShoppingReminder(
        ctx.state.user?.id || 1,
        { items, reminderDate, budget, notes }
      );

      ctx.body = {
        code: 200,
        message: '购物提醒创建成功',
        data: reminder,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 获取购物清单
  async getShoppingList() {
    const { ctx, service } = this;



    try {
      const shoppingList = await service.reminder.getShoppingList(ctx.state.user?.id || 1);

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: shoppingList,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 更新购物清单
  async updateShoppingList() {
    const { ctx, service } = this;



    try {
      const { items } = ctx.request.body;
      
      const shoppingList = await service.reminder.updateShoppingList(
        ctx.state.user?.id || 1,
        items
      );

      ctx.body = {
        code: 200,
        message: '更新成功',
        data: shoppingList,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }
}

module.exports = ReminderController;
